<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改':'添加'"
    :visible="visible"
    width="500px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="140px">
      <el-form-item label="补差依据:" prop="bcname">
        <el-input
          clearable
          v-model="form.bcname"
          placeholder="输入补差依据"/>
      </el-form-item>
      <el-form-item label="适用客户:" prop="invtype">
            <el-select
              clearable
              v-model="form.availableCustomer"
              placeholder="请选择规则适用客户"
              class="ele-fluid">
              <el-option label="全部" value="全部"/>
              <el-option label="医院" value="医院"/>
              <el-option label="商业" value="商业"/>
            </el-select>
      </el-form-item>
      <el-form-item label="产品类型:" prop="invtype">
            <el-select
              clearable
              v-model="form.invtype"
              placeholder="请选择产品类型"
              class="ele-fluid">
              <el-option label="外购" value="外购"/>
              <el-option label="包销" value="包销"/>
            </el-select>
      </el-form-item>
      <el-form-item label="开始日期:" prop="begindate">
        <el-date-picker @change="calcEnddate" v-model="form.begindate" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择开始日期" clearable class="ele-fluid"></el-date-picker>
      </el-form-item>
      <el-form-item label="有效期天:" prop="validity">
        <el-select  
          clearable
          filterable allowCreate
          @change="calcEnddate"
          @blur="selectBlur"
          v-model="form.validity"
          class="ele-fluid">
          <el-option label="15天" :value="15"/>
          <el-option label="30天" :value="30"/>
        </el-select>
      </el-form-item>
      <el-form-item label="业务结束日期:" prop="enddate">
        <el-date-picker disabled v-model="form.enddate" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' class="ele-fluid"></el-date-picker>
      </el-form-item>
      <el-form-item label="销售管理结束日期:" prop="manageEnddate">
        <el-date-picker disabled v-model="form.manageEnddate" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' class="ele-fluid"></el-date-picker>
      </el-form-item>
      <el-form-item label="超期情况说明:" prop="uploadfile">
        <a v-bind:href="baseURL+'uploads/'+form.uploadfile" target="_blank" style="text-decoration: underline;">
          {{form.uploadfile?String(form.uploadfile).slice(String(form.uploadfile).indexOf('_')+1):''}}
        </a>
        <el-upload
          class="ele-block"
          action=""
          v-loading="uploadLoading"
          accept=".pdf,.docx,.doc"
          :show-file-list="false"
          :before-upload="beforeUpload">
          <el-button type="primary" icon="el-icon-upload" class="ele-btn-icon" size="small">点击上传
          </el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import setting  from '@/config/setting';
export default {
  name: 'SpbcInvsetEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: {},
      // 表单验证规则
      rules: {
        bcname: [
          {required: true, message: '请输入补差依据', trigger: 'blur'}
        ],
        invtype: [
          {required: true, message: '请输入产品类型', trigger: 'blur'}
        ],
        begindate: [
          {required: true, message: '请输入开始日期', trigger: 'blur'}
        ],
        validity: [
          {required: true, message: '请输入有效期天', trigger: 'blur'}
        ],
        enddate: [
          {required: true, message: '请输入业务结束日期', trigger: 'blur'}
        ],
        manageEnddate: [
          {required: true, message: '请输入销售管理结束日期', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      uploadLoading: false,
      baseURL: setting.baseURL
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {},
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/spbcinvset/edit' : '/spbcinvset/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    calcEnddate(){
      if(this.form.begindate && this.form.validity){
        var enddate = new Date(this.form.begindate);
        enddate.setDate(enddate.getDate()+Number(this.form.validity));
        var manageEnddate=new Date(enddate);
        manageEnddate.setDate(enddate.getDate()+15);
        this.$set(this.form,'enddate',enddate);
        this.$set(this.form,'manageEnddate',manageEnddate);
      }
    },
    selectBlur(e){
      this.$set(this.form,'validity', e.target.value);
      this.calcEnddate();
    },
    /* 上传 */
    beforeUpload(file) {
      this.uploadLoading = true;
      let formData = new FormData();
      formData.append('file', file);
      this.$http.post('/spbcinvset/uploadfile', formData).then(res => {
        this.uploadLoading = false;
        if (res.data.code === 0) {
          this.$message({type: 'success', message: '附件上传成功'});
          this.form.uploadfile=res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.loading = false;
        this.$uploadLoading.error(e.message);
      });
      return false;
    },
  }
}
</script>

<style scoped>
</style>
